package com.intelligentmeterreadingsys.otherequipment.activity;

import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.OtherEquipmentPOVO;
import com.intelligentmeterreadingsys.waterlist.reviewdata.activity.ReviewDataMainActivity;

import android.app.ActivityGroup;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

/**
 * 设备信息查询
 * @author king
 *
 */
public class EquipementQueryActivityGroup extends ActivityGroup{

	public RadioGroup group;
	public LinearLayout layout;
	public RadioButton radio1;//设备信息
	public RadioButton radio2;//设备列表
	public RadioButton radio3;//设备查询条件
	private String number;
	private String adds;
	private String type;
	private String serchstatus;
	private String querystatus;
	private List<OtherEquipmentPOVO> list;
	private String equipmentid;
	
	public String getEquipmentid() {
		return equipmentid;
	}

	public void setEquipmentid(String equipmentid) {
		this.equipmentid = equipmentid;
	}

	public List<OtherEquipmentPOVO> getList() {
		return list;
	}

	public void setList(List<OtherEquipmentPOVO> list) {
		this.list = list;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getAdds() {
		return adds;
	}

	public void setAdds(String adds) {
		this.adds = adds;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSerchstatus() {
		return serchstatus;
	}

	public void setSerchstatus(String serchstatus) {
		this.serchstatus = serchstatus;
	}

	public String getQuerystatus() {
		return querystatus;
	}

	public void setQuerystatus(String querystatus) {
		this.querystatus = querystatus;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_equipment_querymain);
		init();
	}
	
	public void init(){
		group=(RadioGroup)findViewById(R.id.other_equipment_querymain_group);
		layout=(LinearLayout)findViewById(R.id.other_equipment_layout);
		radio1=(RadioButton)findViewById(R.id.other_equipment_querymain_button0);
		radio2=(RadioButton)findViewById(R.id.other_equipment_querymain_button1);
		radio3=(RadioButton)findViewById(R.id.other_equipment_querymain_button3);
		radio2.setChecked(false);
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==radio1.getId()){
					launchActivity("EquipementInfoActivity", EquipementInfoActivity.class, null);
					EquipementInfoActivity act=(EquipementInfoActivity)getCurrentActivity();
					act.queryEquiment();
				}else if(checkedId==radio2.getId()){
					launchActivity("EquipementQueryListActivity", EquipementQueryListActivity.class, null);
					final EquipementQueryListActivity act=(EquipementQueryListActivity) getCurrentActivity();
					act.flip();
					ListView listview=act.getListView();
					
					listview.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							List<OtherEquipmentPOVO> list=act.getList();
							if(list==null||list.size()<1){
								Toast.makeText(EquipementQueryActivityGroup.this, "没有数据", 1000).show();
								return;
							}
							equipmentid=list.get(arg2).getId();
							radio1.setChecked(true);
							
						}
					});
				}else if(checkedId==radio3.getId()){
					launchActivity("", EquipementConditionActivity.class, null);
					final EquipementConditionActivity act=(EquipementConditionActivity)getCurrentActivity();
					Button button=act.button;
					
					button.setOnClickListener(new android.view.View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							number=act.edit1.getEditableText().toString();
							adds=act.edit2.getEditableText().toString();
							type=act.getType();
							serchstatus=act.getSerchstatus();
							querystatus=act.getQuerystatus();
							radio2.setChecked(true);
						}
					});
				}
			}
		});
		radio2.setChecked(true);
	}

	/**
	 * 界面切换
	 * 
	 * @param id
	 * @param activityClass
	 */
	public void launchActivity(String id, Class<?> activityClass, Bundle bundle) {
		layout.removeAllViews();

		Intent intent = new Intent(EquipementQueryActivityGroup.this, activityClass);
		
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

		Window window = getLocalActivityManager().startActivity(id, intent);
		View view = window.getDecorView();
		layout.addView(view);
	}

	/**
	 * 打开子Activity的菜单事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_MENU){
			getCurrentActivity().openOptionsMenu(); 
		}
		return super.onKeyDown(keyCode, event);
	}
}
